﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace hlavny.modul2
{
    public static class CFunctions
    {
        /// <summary>
        /// Function verify, if the name of column, table or database doesn't contains any forbidden character like separators
        /// </summary>
        /// <param name="name">name to verify</param>
        /// <returns>true if is valid, otherwise false</returns>
        public static bool IsValidName(string name) 
        {
            for (int i = 0; i < name.Length; i++)
            {
                if (char.IsSeparator(name[i]))
                {
                    return false;
                }
            }
            return true;
        }

        #region Serialization and desarialization of objects

        /// <summary>
        /// Function serialize CSysColumns object to file
        /// </summary>
        /// <param name="obj">object to serialize</param>
        public static void StoreSysColumns(CSysColumns obj)
        {
            FileStream stream = new FileStream(CConfig.SysColumnsFileName, FileMode.Create);
            BinaryFormatter binary = new BinaryFormatter();
            binary.Serialize(stream, obj);
            stream.Close();
        }

        /// <summary>
        /// Function deserialize CSysColumns object from file to reference object
        /// </summary>
        /// <param name="obj">reference to CSysColumns object </param>
        public static void LoadSysColumns(ref CSysColumns obj)
        {
            FileStream stream = new FileStream(CConfig.SysColumnsFileName, FileMode.Open);
            BinaryFormatter binary = new BinaryFormatter();
            obj = binary.Deserialize(stream) as CSysColumns;
            stream.Close();
        }

        /// <summary>
        /// Function serialize CSysTables object to file
        /// </summary>
        /// <param name="obj">object to serialize</param>
        public static void StoreSysTables(CSysTables obj)
        {
            FileStream stream = new FileStream(CConfig.SysTablesFileName, FileMode.Create);
            BinaryFormatter binary = new BinaryFormatter();
            binary.Serialize(stream, obj);
            stream.Close();
        }

        /// <summary>
        /// Function deserialize CSysTables object from file to reference object
        /// </summary>
        /// <param name="obj">reference to CSysTables object </param>
        public static void LoadSysTables(ref CSysTables obj)
        {
            FileStream stream = new FileStream(CConfig.SysTablesFileName, FileMode.Open);
            BinaryFormatter binary = new BinaryFormatter();
            obj = binary.Deserialize(stream) as CSysTables;
            stream.Close();
        }

        /// <summary>
        /// Function serialize CSysDatabases object to file
        /// </summary>
        /// <param name="obj">object to serialize</param>
        public static void StoreSysDatabases(CSysDatabases obj)
        {
            FileStream stream = new FileStream(CConfig.SysDatabasesFileName, FileMode.Create);
            BinaryFormatter binary = new BinaryFormatter();
            binary.Serialize(stream, obj);
            stream.Close();
        }

        /// <summary>
        /// Function deserialize CSysDatabases object from file to reference object
        /// </summary>
        /// <param name="obj">reference to CSysDatabases object </param>
        public static void LoadSysDatabases(ref CSysDatabases obj)
        {
            FileStream stream = new FileStream(CConfig.SysDatabasesFileName, FileMode.Open);
            BinaryFormatter binary = new BinaryFormatter();
            obj = binary.Deserialize(stream) as CSysDatabases;
            stream.Close();
        } 

        #endregion
    }
}
